package com.xm.dao.background;

import com.xm.entity.CommoditySpecificationType;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品类别
 * @author 康文程
 * @date 2024-1-17
 */
public class CommoditySpecificationTypeDao extends BaseUtil {
    /**
     * 新增商品规格类型
     *
     * @param specificationTypeName 规格类型名称
     * @return 新增成功返回true，失败返回false
     * @throws SqlErrorException SQL执行异常
     */
    public boolean insertCommoditySpecificationType(String specificationTypeName) throws SqlErrorException {
        @Language("SQL")
        String sql = "INSERT INTO commodity_specification_type (specification_type_name) VALUES (?)";
        Object[] params = {specificationTypeName};
        int result = executeUpdate(sql, params);
        return result > 0;
    }
    /**
     * 查询全部商品规格类别
     *
     * @return 商品规格类别
     * @throws SqlErrorException SQL执行异常
     */
    public List<CommoditySpecificationType> getAllCommoditySpecificationTypes() throws SqlErrorException {
        @Language("SQL")
        String sql = "SELECT * FROM commodity_specification_type";
        ResultSet rs = null;
        List<CommoditySpecificationType> types = new ArrayList<>();
        try {
            rs = executeQuery(sql);
            while (rs.next()) {
                CommoditySpecificationType type = new CommoditySpecificationType();
                type.setSpecificationTypeId(rs.getInt("specification_type_id"));
                type.setSpecificationTypeName(rs.getString("specification_type_name"));
                types.add(type);
            }
        } catch (SQLException e) {
            throw new SqlErrorException("SQL查询异常", e);
        } finally {
            close(rs);
            close(null);
        }
        return types;
    }
}
